//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.WPF;
using Microsoft.Practices.CompositeUI.Services;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.ObjectBuilder;

using LitwareHR.PublicApp.WinUX.Constants;
using LitwareHR.PublicApp.WinUX.Properties;
using LitwareHR.PublicApp.WinUX.Interface.Services;
using LitwareHR.Infrastructure.Interface.Constants;

using System.Windows.Forms;
   
namespace LitwareHR.PublicApp.WinUX.Services
{
    public class TenantInfo
    {
        private Guid _id;
        private string _alias;

        public Guid Id
        { get { return _id; } }

        public string Alias
        { get { return _alias; } }

        public TenantInfo(string alias, Guid id)
        {
            _id = id;
            _alias = alias;
        }
    }

    public class AuthenticationService : IAuthenticationSC 
    {
        private WorkItem WorkItem = null;
        private Guid _currentTenantId;
        private string _currentTenantAlias = "";
        private TenantInfo _currentTenant;

        public AuthenticationService([ServiceDependency] WorkItem  WorkItem )
        {
            this.WorkItem = WorkItem;
        }

        public bool IsAuthenticated()
        {
            if (_currentTenant == null)
                return false;
            else
                return true;
        }

        #region IAuthenticationSC Members

        public bool Authenticate()
        {
            Properties.Settings.Default.SecurityToken = String.Empty;

            WindowSmartPartInfo spInfo = new WindowSmartPartInfo();
            spInfo.Title = "Litware HR - Login";
            spInfo.Width = 365;
            spInfo.Height = 275;
            spInfo.MaximizeBox = false;
            spInfo.MinimizeBox = false;
            spInfo.Description = "Login Screen: Please enter your Alias, Username & Password";
            spInfo.Modal = true;
            LoginView loginView = WorkItem.SmartParts.AddNew<LoginView>();
            WorkItem.Workspaces[WorkspaceNames.ModalWindows].Show(loginView, spInfo); 

            bool isAuthenticated = Properties.Settings.Default.SecurityToken != String.Empty;

            LoadCurrentTenantInfo(isAuthenticated);

            return isAuthenticated;
        }

        private void LoadCurrentTenantInfo(bool isAuthenticated)
        {
            if (isAuthenticated)
            {
                _currentTenantAlias = Properties.Settings.Default.TenantAlias;
                _currentTenantId = new Guid(Properties.Settings.Default.TenantId);
                _currentTenant = new TenantInfo(_currentTenantAlias, _currentTenantId);
            }
        }

        public string CurrentTenantAlias
        {
            get { return _currentTenantAlias; }
        }

        public Guid CurrentTenantId
        {
            get { return _currentTenantId;  }
        }

        public TenantInfo CurrentTenant
        {
            get { return _currentTenant; }
        }


        #endregion
    }
}
